package com.jay.tools.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;

/**
 * Created by ouyangbin on 2015/9/10.
 */
public class StreamUtils {

    final static int BUFFER_SIZE = 1024;

    private StreamUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static String readLine(PushbackInputStream in) throws IOException {
        char buf[] = new char[128];
        int room = buf.length;
        int offset = 0;
        int c;
        loop:
        while (true) {
            switch (c = in.read()) {
                case -1:
                case '\n':
                    break loop;
                case '\r':
                    int c2 = in.read();
                    if ((c2 != '\n') && (c2 != -1)) in.unread(c2);
                    break loop;
                default:
                    if (--room < 0) {
                        char[] lineBuffer = buf;
                        buf = new char[offset + 128];
                        room = buf.length - offset - 1;
                        System.arraycopy(lineBuffer, 0, buf, 0, offset);

                    }
                    buf[offset++] = (char) c;
                    break;
            }
        }
        if ((c == -1) && (offset == 0)) return null;
        return String.copyValueOf(buf, 0, offset);
    }

    /**
     * InputStream 转为 ByteArray
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] inStream2ByteArray(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        byte[] bytes = outSteam.toByteArray();
        outSteam.close();
        inStream.close();
        return bytes;
    }

    /**
     * InputStream 转为 String
     */
    public static String inputStream2Str(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[BUFFER_SIZE];
        int len = -1;
        while ((len = inStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
            outStream.write(buffer, 0, len);
        }
        String str = new String(outStream.toByteArray(), "UTF-8");
        outStream.close();
        inStream.close();
        return str;

    }

    /**
     * 把Stream转换成String
     */
    public static String inputStream2String(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;

        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    /**
     * 将byte数组转换成String
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String byte2String(byte[] bytes) throws Exception {
        InputStream inputStream = byte2InputStream(bytes);
        return inputStream2Str(inputStream);
    }

    /**
     * String 转为 InputStream
     *
     * @param text
     * @return
     * @throws Exception
     */
    public static InputStream str2InputStream(String text) throws Exception {
        return new ByteArrayInputStream(text.getBytes("UTF-8"));
    }

    /**
     * byte数组 转成 InputStream
     *
     * @param in
     * @return
     * @throws Exception
     */
    public static InputStream byte2InputStream(byte[] in) throws Exception {
        return new ByteArrayInputStream(in);
    }


}
